package com.xt.website_manager.controller;


import com.xt.website_manager.po.Category;
import com.xt.website_manager.po.CategoryExt;
import com.xt.website_manager.po.CategorySecond;
import com.xt.website_manager.service.CategoryService;
import com.xt.website_manager.service.NewsService;
import com.xt.website_manager.service.ProductService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.jws.WebParam;
import javax.servlet.ServletContext;
import java.util.ArrayList;
import java.util.List;

/*
 * @Author 爱尚实训赵国鑫
 * @ClassName  CategoryController
 * @Date 2019/8/18/10:13
 * @Description TODO:(一句话描述这个类)
 */
@Controller
@RequestMapping("/category")
public class CategoryController {
    @Resource
    private ServletContext applicationContext;
    @Resource
    private CategoryService categoryService;
    @Resource
    private ServletContext application;
    @Resource
    private NewsService newsService;
    @Resource
    private ProductService productService;
    /**
     * 获取全部一二级类目
     *
     * @return
     */
    @RequestMapping("/findAllCategories.html")
    public ArrayList<CategoryExt<CategorySecond>> findAllCategories() {
        ArrayList<CategoryExt<CategorySecond>> allCategories = (ArrayList<CategoryExt<CategorySecond>>) application.getAttribute("allCategories");

        if (allCategories == null) {
            allCategories = categoryService.getAllCategories();
            application.setAttribute("allCategories", allCategories);
        }
        return allCategories;
    }

    /**
     * 获取全部一级类目
     */
    @RequestMapping("/findCategories.html")
    public List<Category> findCategories() {
        return categoryService.getCategories();
    }

    /**
     * 获取全部一级类目
     */
    @RequestMapping("/findCategoriesToJson")
    @ResponseBody
    public List<Category> findCategoriesToJson() {
        return categoryService.getCategories();
    }


    /**
     * 根据一级类目id获取全部二级类目
     */
    @RequestMapping("/findCategorySecondByCid.html")
    public List<CategorySecond> findCategorySecondByCid(Integer cid) {
        return categoryService.findCategorySecondByCid(cid);
    }

    @RequestMapping("/findCategorySecondByCidToJson")
    @ResponseBody
    public List<CategorySecond> findCategorySecondByCidToJson(Integer cid) {
        return categoryService.findCategorySecondByCid(cid);
    }


    /**
     * 跳转到类目管理页
     */
    @RequestMapping("/toCategory.html")
    public String category(Model model){
        List<Category> categories = categoryService.getCategories();
        model.addAttribute("categories",categories);
        return "category";
    }

    /**
     * 跳转到修改一级类目页
     */
    @RequestMapping("/toUpdateCategory.html")
    public String toUpdateCategory(Integer cid,Model model){
        //回显一级类目
          Category category = categoryService.getCategoryByCid(cid);
          model.addAttribute("category",category);
        return "updateCategory";
    }


    /**
     * 修改一级类目信息
     */
    @RequestMapping("/updateCategory.html")
    public String updateCategory(@RequestParam("pic") MultipartFile file,Category category,String prevPath,Boolean changeType){
        if(changeType){
            System.out.println("==========");
            categoryService.updateCategorySecondType(category);
        }
        categoryService.updateCategory(category,file,prevPath,applicationContext.getRealPath("/images"));
        return "redirect:/category/toCategory.html";
    }

    /**
     * 去往添加一级类目
     */
    @RequestMapping("/toAddCategory.html")
    public String toAddCategory(){
        return "addCategory";
    }

    /**
     * 添加一级类目
     */
    @RequestMapping("/addCategory.html")
    public String addCategory(@RequestParam("pic") MultipartFile file,Category category){
        categoryService.addCategory(category,file, applicationContext.getRealPath("/images"));
        return "redirect:/category/toCategory.html";
    }
    /**
     * 删除一级类目
     */
    @RequestMapping("/delCategory.html")
    @ResponseBody
    public String delCategory(Integer id){
        Integer count = categoryService.findCategoryCountByCid(id);
        if(count==0){
            categoryService.delCategory(id,applicationContext.getRealPath(""));
            return "ok";
        }else{
            return "no";
        }
    }
    /**
     * 更改一级类目优先级
     */
    @RequestMapping("/changeSort.html")
    @ResponseBody
    public String changeSort(Category category){
        categoryService.changeCategorySort(category);
       return "ok";
    }






    /**
     *去往二级类目管理页
     */
    @RequestMapping("/toCategorySecond.html")
    public String toCategorySecond(Model model,Integer cid){
        if(cid==null){
            return "redirect:/category/toCategory.html";
        }else{
            CategoryExt<CategorySecond> categoryExt = categoryService.findCategoryByCid(cid);
            model.addAttribute("categoryExt",categoryExt);
            return "categorySecond";
        }
    }

    /**
     * 去往添加二级类目
     */
    @RequestMapping("/toAddCategorySecond.html")
    public String toAddCategorySecond(){
        return "addCategorySecond";
    }

    /**
     * 添加二级类目
     */
    @RequestMapping("/addCategorySecond.html")
    public String addCategorySecond(@RequestParam("pic") MultipartFile file,CategorySecond categorySecond){
        categoryService.addCategorySecond(categorySecond,file,applicationContext.getRealPath("/images"));
        return "redirect:/category/toCategorySecond.html?cid="+categorySecond.getCid()+"&type="+categorySecond.getType();
    }


    /**
     * 删除二级类目
     */
    @RequestMapping("/delCategorySecond.html")
    @ResponseBody
    public String delCategorySecond(Integer csid,Integer type){
        Integer count = 0;
        if(type==0){//查询产品表
           count = productService.getNewsCount(csid);
        }else{//查询新闻表
           count = newsService.getNewsCount(csid);
        }
        if(count==0){
            categoryService.delCategorySecond(csid,applicationContext.getRealPath(""));
            return "ok";
        }else{
            return "no";
        }
    }

    /**
     * 更改二级类目优先级
     */
    @RequestMapping("/changeCsSort.html")
    @ResponseBody
    public String changeSort(CategorySecond categorySecond){
        categoryService.changeCategoryCsSort(categorySecond);
        return "ok";
    }

    /**
     * 去往修改二级类目页面
     */
    @RequestMapping("/toUpdateCategorySecond.html")
    public String toUpdateCategorySecond(Integer id,Model model){
        //回显二级类目
            CategorySecond second = categoryService.getCategorySecond(id);
            model.addAttribute("categorySecond",second);
        return "updateCategorySecond";
    }


    /**
     * 修改二级类目
     */
    @RequestMapping("/updateCategorySecond.html")
    public String updateCategoryScond(@RequestParam("pic") MultipartFile file,CategorySecond categorySecond,String prevPath){
        categoryService.updateCategorySecond(categorySecond,file,prevPath,applicationContext.getRealPath("/images"));
        return "redirect:/category/toCategorySecond.html?cid="+categorySecond.getCid()+"&type="+categorySecond.getType();
    }
}
